home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / htmldrv.cpp < prev    next >
C/C++ Source or Header  |  1999-03-30  |  11KB  |  471 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: htmldrv.cpp 
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: gaer@nhc.noaa.gov
  8. // File Creation Date: 03/09/1999 
  9. // Date Last Modified: 03/31/1999
  10. // ----------------------------------------------------------- // 
  11. // ------------- Program description and details ------------- // 
  12. // ----------------------------------------------------------- // 
  13. /*
  14. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  15. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  16. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  17. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  18. CORRECTION.
  19.   
  20. The HyperTextDrv and HyperText class is used to create HTML
  21. documents. The HyperTextDrv class is a base class that uses
  22. the C++ ostream library to write HTML tags and text to a
  23. specified stream. The HyperText class is used to write
  24. HTML document templates to a specified stream,
  25. */
  26. // ----------------------------------------------------------- // 
  27. #include <string.h>
  28. #include <stdio.h>
  29. #include <time.h>
  30. #include "htmldrv.h"
  31.  
  32. // Define program constants
  33. const char *DefaultTitle = "HTML Document";
  34.  
  35. const char *htmColors[NumHTMLColors] = {
  36.   "\"#000000\"", // htmBLACK       
  37.   "\"#000080\"", // htmDARKBLUE    
  38.   "\"#0000ff\"", // htmBLUE        
  39.   "\"#008000\"", // htmGREEN       
  40.   "\"#008080\"", // htmTEAL        
  41.   "\"#00ff00\"", // htmBRIGHTGREEN 
  42.   "\"#00ffff\"", // htmTURQUOISE   
  43.   "\"#800000\"", // htmDARKRED     
  44.   "\"#800080\"", // htmVIOLET      
  45.   "\"#808000\"", // htmDARKYELLOW  
  46.   "\"#808080\"", // htmDARKGRAY    
  47.   "\"#C0C0C0\"", // htmGRAY        
  48.   "\"#ff0000\"", // htmRED         
  49.   "\"#ff00ff\"", // htmPINK        
  50.   "\"#ffff00\"", // htmYELLOW      
  51.   "\"#ffffff\""  // htmWHITE       
  52. };
  53.  
  54. const char *htmFonts[NumHTMLFonts] = {
  55.   "Arial",        // htmARIAL,
  56.   "Arial Black",  // htmARIALBLACK
  57.   "Arial Narrow", // htmARIALNARROW
  58.   "Courier New"   // htmCOURIER,
  59. };
  60.  
  61. HyperTextDrv::HyperTextDrv(ostream &s)
  62. {
  63.   stream = &s;
  64.   dec_precision = DefaultPrecision;
  65.   non_breaking_sp = 0; // Do not use non-breaking spaces by default
  66. }
  67.  
  68. HyperTextDrv::~HyperTextDrv()
  69. {
  70.   // Destructor provided for virtuality
  71. }
  72.  
  73. void HyperTextDrv::WriteString(const char *s)
  74. // Write a single line of text to the stream, filtering all
  75. // the characters that have special meaning in HTML documents.
  76. {
  77.  char *p = (char *)s;
  78.  unsigned len = strlen(s);
  79.  
  80.  while(len--) {
  81.    WriteChar((const unsigned char)*p);
  82.    p++;
  83.  }
  84. }
  85.  
  86. void HyperTextDrv::WriteChar(const unsigned char c) const
  87. // Write a single character to the stream, filtering all the
  88. // characters that have special meaning in HTML documents.
  89. {
  90.   switch(c) {
  91.     case '<' : // Less than sign
  92.       *(stream) << "<"; 
  93.       break;
  94.  
  95.     case '>': // Greater then sign
  96.       *(stream) << ">";  
  97.       break;
  98.  
  99.     case '&' : // Ampersand
  100.       *(stream) << "&"; 
  101.       break;
  102.  
  103.     case ' ' : // Treat spaces as breaking or non-breaking
  104.       if(non_breaking_sp)
  105.     *(stream) << " ";
  106.       else
  107.     *(stream) << c;
  108.       break;
  109.  
  110.     default:  
  111.       int ex_set = c;
  112.       if(ex_set >= 127) { // Fitler the extended ASCII character set
  113.     *(stream) << "&#" << ex_set;
  114.     break;
  115.       }
  116.       *(stream) << c;
  117.       break;
  118.   }
  119. }
  120.  
  121. void HyperTextDrv::Write(char c)
  122. {
  123.   WriteChar((const unsigned char)c);
  124. }
  125.  
  126. void HyperTextDrv::Write(const char c) const
  127. {
  128.   WriteChar((const unsigned char)c);
  129. }
  130.  
  131. void HyperTextDrv::Write(unsigned char c)
  132. {
  133.   WriteChar((const unsigned char)c);
  134. }
  135.  
  136. void HyperTextDrv::Write(const unsigned char c) const
  137. {
  138.   WriteChar(c);
  139. }
  140.  
  141. void HyperTextDrv::Write(char *s)
  142. {
  143.   WriteString((const char *)s);
  144. }
  145.  
  146. void HyperTextDrv::Write(const char *s)
  147. {
  148.   WriteString(s);
  149. }
  150.  
  151. void HyperTextDrv::Write(unsigned char *s)
  152. {
  153.   WriteString((const char *)s);
  154. }
  155.  
  156. void HyperTextDrv::Write(const unsigned char *s)
  157. {
  158.   WriteString((const char *)s);
  159. }
  160.  
  161. void HyperTextDrv::Write(const long val) const
  162. {
  163.   *(stream) << val;
  164. }
  165.  
  166. void HyperTextDrv::Write(long val)
  167. {
  168.   *(stream) << val;
  169. }
  170.  
  171. void HyperTextDrv::Write(const unsigned long val) const
  172. {
  173.   *(stream) << val;
  174. }
  175.  
  176. void HyperTextDrv::Write(unsigned long val)
  177. {
  178.   *(stream) << val;
  179. }
  180.  
  181. void HyperTextDrv::Write(const int val) const
  182. {
  183.   *(stream) << val;
  184. }
  185.  
  186. void HyperTextDrv::Write(int val)
  187. {
  188.   *(stream) << val;
  189. }
  190.  
  191. void HyperTextDrv::Write(const unsigned int val) const
  192. {
  193.   *(stream) << val;
  194. }
  195.  
  196. void HyperTextDrv::Write(unsigned int val)
  197. {
  198.   *(stream) << val;
  199. }
  200.  
  201. void HyperTextDrv::Write(double val)
  202. {
  203.   stream->setf(ios::showpoint | ios::fixed);
  204.   stream->precision(dec_precision);
  205.   *(stream) << val;
  206. }
  207.  
  208. void HyperTextDrv::Write(const double val) const
  209. {
  210.   stream->setf(ios::showpoint | ios::fixed);
  211.   stream->precision(dec_precision);
  212.   *(stream) << val;
  213. }
  214.  
  215. void HyperTextDrv::Write(float val)
  216. {
  217.   stream->setf(ios::showpoint | ios::fixed);
  218.   stream->precision(dec_precision);
  219.   *(stream) << val;
  220. }
  221.  
  222. void HyperTextDrv::Write(const float val) const
  223. {
  224.   stream->setf(ios::showpoint | ios::fixed);
  225.   stream->precision(dec_precision);
  226.   *(stream) << val;
  227. }
  228.  
  229. ostream& HyperTextDrv::operator<<(ostream & (*_f)(ostream&))
  230. {
  231.   (*_f)(*(stream));
  232.   return *(stream);
  233. }
  234.  
  235. HyperTextDrv& HyperTextDrv::operator<<(char *s)
  236. {
  237.   Write(s);
  238.   return *this;
  239. }
  240.  
  241. HyperTextDrv& HyperTextDrv::operator<<(const char *s)
  242. {
  243.   Write(s);
  244.   return *this;
  245. }
  246.  
  247. HyperTextDrv& HyperTextDrv::operator<<(unsigned char *s)
  248. {
  249.   Write(s);
  250.   return *this;
  251. }
  252.  
  253. HyperTextDrv& HyperTextDrv::operator<<(const unsigned char *s)
  254. {
  255.   Write(s);
  256.   return *this;
  257. }
  258.  
  259. HyperTextDrv& HyperTextDrv::operator<<(char c)
  260. {
  261.   Write(c);
  262.   return *this;
  263. }
  264.  
  265. const HyperTextDrv& HyperTextDrv::operator<<(const char c) const
  266. {
  267.   Write(c);
  268.   return *this;
  269. }
  270.  
  271. HyperTextDrv& HyperTextDrv::operator<<(unsigned char c)
  272. {
  273.   Write(c);
  274.   return *this;
  275. }
  276.  
  277. const HyperTextDrv& HyperTextDrv::operator<<(const unsigned char c) const
  278. {
  279.   Write(c);
  280.   return *this;
  281. }
  282.  
  283. HyperTextDrv& HyperTextDrv::operator<<(long val)
  284. {
  285.   Write(val);
  286.   return *this;
  287. }
  288.  
  289. const HyperTextDrv& HyperTextDrv::operator<<(const long val) const
  290. {
  291.   Write(val);
  292.   return *this;
  293. }
  294.  
  295. HyperTextDrv& HyperTextDrv::operator<<(unsigned long val)
  296. {
  297.   Write(val);
  298.   return *this;
  299. }
  300.  
  301. const HyperTextDrv& HyperTextDrv::operator<<(const unsigned long val) const
  302. {
  303.   Write(val);
  304.   return *this;
  305. }
  306.  
  307. HyperTextDrv& HyperTextDrv::operator<<(int val) 
  308. {
  309.   Write(val);
  310.   return *this;
  311. }
  312.  
  313. const HyperTextDrv& HyperTextDrv::operator<<(const int val) const
  314. {
  315.   Write(val);
  316.   return *this;
  317. }
  318.  
  319. HyperTextDrv& HyperTextDrv::operator<<(unsigned int val)
  320. {
  321.   Write(val);
  322.   return *this;
  323. }
  324.  
  325. const HyperTextDrv& HyperTextDrv::operator<<(const unsigned int val) const
  326. {
  327.   Write(val);
  328.   return *this;
  329. }
  330.  
  331. const HyperTextDrv& HyperTextDrv::operator<<(const float val) const
  332. {
  333.   Write(val);
  334.   return *this;
  335. }
  336.  
  337. HyperTextDrv& HyperTextDrv::operator<<(float val)
  338. {
  339.   Write(val);
  340.   return *this;
  341. }
  342.  
  343. const HyperTextDrv& HyperTextDrv::operator<<(const double val) const
  344. {
  345.   Write(val);
  346.   return *this;
  347. }
  348.  
  349. HyperTextDrv& HyperTextDrv::operator<<(double val)
  350. {
  351.   Write(val);
  352.   return *this;
  353. }
  354.  
  355. void HyperText::Prologue(const char *doc_title)
  356. {
  357.   *(stream) << html << endl;
  358.   *(stream) << head << endl;
  359.   if(doc_title)
  360.     *(stream) << title << ' ' << doc_title << ' ' << etitle << endl;
  361.   else
  362.     *(stream) << title << ' ' << DefaultTitle << ' ' << etitle << endl;
  363.   *(stream) << ehead << endl;
  364. }
  365.  
  366. void HyperText::StartBody(const char *parameters)
  367. {
  368.   if(parameters == 0)
  369.     *(stream) << body;
  370.   else
  371.     BODY(parameters);
  372.  
  373.   *(stream) << endl;
  374. }
  375.  
  376. void HyperText::StartBody(htmCOLORS color)
  377. {
  378.   *(stream) << lt << "BODY BGCOLOR=" << htmColors[color] << gt << endl;
  379.   *(stream) <<